package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.FunctionTime;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author Demon
 * 获取多功能卡事件
 * @date 24/5/2024  下午7:31
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class GetFunctionTime implements CompleteEntityScheduleActions {
	
	@Override
	public ByteString objectConvertBytes() {
		final CompleteInstruction.GetFunctionTime.InputMsg.Builder inputMsgBuilder =
				CompleteInstruction.GetFunctionTime.InputMsg.newBuilder();
		GetFunctionTime.InputMsg thisInput = this.getInputMsg();
		inputMsgBuilder.setPrcIndex(thisInput.getPrcIndex());
		inputMsgBuilder.setPortIndex(thisInput.getPortIndex());
		inputMsgBuilder.setFunctionIndex(thisInput.getFunctionIndex());
		
		final CompleteInstruction.GetFunctionTime.Builder builder =
				CompleteInstruction.GetFunctionTime.newBuilder();
		builder.setInput(inputMsgBuilder);
		return builder.build().toByteString();
	}
	
	@Override
	public GetFunctionTime byteConvertObject(ByteString bytes) {
		CompleteInstruction.GetFunctionTime getFunctionTime = null;
		try {
			getFunctionTime = CompleteInstruction.GetFunctionTime.parseFrom(bytes);
		} catch (InvalidProtocolBufferException e) {
			log.error("", e);
		}
		if (Objects.isNull(getFunctionTime)) {
			throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
		}
		
		GetFunctionTime.OutputMsg outputMsg = new GetFunctionTime.OutputMsg();
		outputMsg.setFunctionTime(new FunctionTime().formProto(getFunctionTime.getOutput().getFunctionTime()));
		this.setOutputMsg(outputMsg);
		return this;
	}
	
	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	@Builder
	public static class InputMsg implements Serializable {
		/**
		 * 发送卡序号
		 * 网卡号
		 * 多功能卡序号
		 */
		private Integer prcIndex;
		private Integer portIndex;
		private Integer functionIndex;
	}
	
	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	@Builder
	public static class OutputMsg implements Serializable {
		/**
		 * 返回设置后的时间
		 */
		private FunctionTime functionTime;
	}
	
	private InputMsg inputMsg;
	private OutputMsg outputMsg;
	
}
